

# 8.1 调用不需要变量的函数

# 8.1.1 变量 def f(x), p(x), 调用的时候，f(a)


def greet_user(username):
    """显示简单的问候语""" ## 注释问题，看一眼PEP 8
    print("Hello!")
    print("Hello, "+username.title()+"!")
greet_user('sarah')

# 8.2 f(x,y)

def describe_pet(animal_type, pet_name):
    """显示宠物信息"""
    print("\nI have a "+animal_type+'.')
    print("My "+animal_type+"'s name is "+pet_name.title()+'.')

describe_pet('hamster','harry')
## 默认传递参数的顺序

describe_pet('dog','whillie')
## 函数嘛，定义一次之后，可以多次调用

describe_pet(animal_type='hamester',pet_name='harry')
## 可以指定给参数传递值，不用考虑顺序
describe_pet(pet_name='harry',animal_type='hamester')

# 设定参数默认值

def describe_pet(pet_name, animal_type='dog'):
    """显示宠物信息"""
    print("\nI have a "+animal_type+'.')
    print("My "+animal_type+"'s name is "+pet_name.title()+'.')

describe_pet(pet_name='whillie')
describe_pet(pet_name='yuanyuan',animal_type='panda')

# 参数不对劲的时候，会报错
# describe_pet()


# 8.3 返回值

## 返回一个字符串

def get_formatted_name(first_name, last_name):
    """返回姓名"""
    full_name= first_name+" "+last_name
    return full_name.title()

musician=get_formatted_name('jimi','hendrix')
print(musician)

## 也许有的人有中间名，有的人没有，可选择性输入的参数

def get_formatted_name(first_name, last_name,middle_name=''):
    """返回姓名"""
    if middle_name:
        full_name= first_name+" "+middle_name+' '+last_name
    else:
        full_name = first_name + " " + last_name
    return full_name.title()

musician=get_formatted_name('jimi','hendrix')
print(musician)
musician=get_formatted_name('jimi','hendrix','lee')
print(musician)

# 返回字典

def build_person(first_name, last_name, age=''):
    person={'first':first_name,'last':last_name}
    if age:
        person['age']=age
    return person
musician=build_person('jimi','hendrix',age=27)
print(musician)

# 结合函数使用while循环

def get_formatted_name(first_name, last_name):
    full_name=first_name+' '+last_name
    return full_name.title()

while True:
    print("\nPlease tell me your name:")
    print("(enter 'q' at any time to quit)")

    f_name=input("First name:")
    if f_name=='q':
        break

    l_name=input("Last name:")
    if l_name=='q':
        break

    formatted_name=get_formatted_name(f_name,l_name)
    print("\nHello, "+formatted_name+"!")

# 8.4 传递列表，就是你要给变量传递的值有一排

def greet_users(names):
    """向列表中的每位用户都发出简单的问候"""
    for name in names:
        msg="Hello, "+name.title()+"!"
        print(msg)

usernames=['hanna','tw','we']
greet_users(usernames)


## 转移列表功能，抽象出来
unprinted_designs=['a','b','c','d']
completed_models=[]

while unprinted_designs:
    current_design=unprinted_designs.pop()

    print("Printing model: "+current_design)
    completed_models.append(current_design)

print("\nThe following models have been printed:")
for completed_model in completed_models:
    print(completed_model)

## 用两个函数实现列表转移功能

def print_models(unprinted_designs,completed_models):
    while unprinted_designs:
        current_design=unprinted_designs.pop()
        print("Printing model: "+current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

# unprinted_designs=['a','b','c','d']
# # 创建一个列表，其中包含一些要打印的信息
# completed_models=[]

# print_models(unprinted_designs,completed_models)
# show_completed_models(completed_models)
# show_completed_models(unprinted_designs)

## 如果不想掏空原来的列表，只想复制，那用切片进行引用
unprinted_designs=['iphone case','robot pendant','dodecahedron']
completed_models=[]

print_models(unprinted_designs[:],completed_models)
show_completed_models(unprinted_designs)
show_completed_models(completed_models)
## 上面的操作要注释掉




# 8.5 任意数量的实参
def make_pizza(*toppings):
    print('我要一个酱婶儿的披萨'+str(toppings))
make_pizza('pepperoni')
make_pizza("mushrooms",'green peppers','extra cheese')

#for循环引入任意数量实参
def make_pizza(*toppings):
    print("\nMaking a pizza with the following toppings:")
    for topping in toppings:
        print("- "+topping)
make_pizza('pepperoni')
make_pizza("mushrooms",'green peppers','extra cheese')

#位置实参+任意数量实参,实际是元组参数
def make_pizza(size,*toppings):
    print('\nMaking a '+str(size)+"-inch pizza with the following toppings:")
    for topping in toppings:
        print("- "+topping)
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')

#字典参数
def build_profile(first, last, **user_info):
    profile={}
    profile['first_name']=first
    profile['last_name']=last
    for key, value in user_info.items():
        profile[key]=value
    return profile
user_profile=build_profile('albert','einstein',location='princeton',field='physics')
print(user_profile)

# 8.6 将函数储存在模块中

#调用函数

import sys
sys.path.append("D:\Python练习") ## 一定要有这个！！！

import pizza
pizza.make_pizza(16,'pepperoni')
pizza.make_pizza(16,'pepperoni','green peppers','extra cheese')

#调用特定函数
import sys
sys.path.append("D:\Python练习") ## 一定要有这个！！！

from pizza import make_pizza as mp

mp(16,'pepperoni')

#给调用的模块指定名

import sys
sys.path.append("D:\Python练习") ## 一定要有这个！！！
import pizza as p
p.make_pizza(16,'pepperoni')

#导入模块中所有函数
import sys
sys.path.append("D:\Python练习")

from pizza import *
make_pizza(16,'pepperoni')
make_pizza(21,'mushrooms','green peppers','extra cheese')

# # 8.7 函数编写指南，看眼书，PEP 8
#

a='nihao'
help(a.title)
dir


